package cn.fxl.service.impl;

import cn.fxl.ajax.AjaxResult;
import cn.fxl.domain.BuyMovieDto;
import cn.fxl.domain.MoveUser;
import cn.fxl.domain.Movie;
import cn.fxl.domain.User;
import cn.fxl.feign.UserFeign;
import cn.fxl.mapper.MovieMapper;
import cn.fxl.mapper.MovieUserMapper;
import cn.fxl.service.IMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class MovieServiceImpl implements IMovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private MovieUserMapper movieUserMapper;
    @Autowired
    private UserFeign userFeign;
    @Override
    public List<Movie> getAll() {
        return movieMapper.getAll();
    }

    @Override
    public AjaxResult buymovie(BuyMovieDto buyMovieDto) {
        Long movieId = buyMovieDto.getMovieId();
        Long userId = buyMovieDto.getUserId();
       Movie movie= movieMapper.getOneByMovieId(movieId);
       if(movie.getStock()==null||movie.getStock().equals(0)){
           return new AjaxResult(false,"电影票不足");
       }
        MoveUser moveUser = new MoveUser();
        User user = new User();
        user.setId(userId);
        user.setBalance(movie.getPrice());
        moveUser.setUser_id(userId);
        moveUser.setMovie_ticket_id(movieId);
        moveUser.setStatus(0);
        userFeign.buyticket(user);
        movieUserMapper.save(moveUser);

        return new AjaxResult();
    }

    @Override
    public Movie getOne(Long id) {
        return movieMapper.getOne(id);
    }

    @Override
    public void UpdateByMovie(Movie movie) {
        movieMapper.UpdateByMovie(movie);
    }
}
